package com.dtp.core.spring;

import org.apache.commons.lang3.BooleanUtils;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

import static com.dtp.common.constant.DynamicTpConst.DTP_ENABLED_PROP;

/**
 * 动态线程池配置选择器，选择在Spring启动时加载DtpBaseBeanConfiguration和DtpBeanDefinitionRegistrar
 *
 * @author KamTo Hung
 * @since 1.1.1
 *
 * @remark muse
 */
public class DtpConfigurationSelector implements DeferredImportSelector, Ordered, EnvironmentAware {

    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {this.environment = environment;}

    @Override
    public String[] selectImports(AnnotationMetadata metadata) {
        // 如果"spring.dynamic.tp.enabled"是false，则返回空的字符串数组
        if (!BooleanUtils.toBoolean(environment.getProperty(DTP_ENABLED_PROP, BooleanUtils.TRUE)))
            return new String[]{};

        // 返回需要初始化的bean名称集合
        return new String[]{DtpBaseBeanConfiguration.class.getName(), DtpBeanDefinitionRegistrar.class.getName()};
    }

    @Override
    public int getOrder() {return HIGHEST_PRECEDENCE;} // 最高执行级别
}
